import com.boe.rnmf.common.util.OsInfo;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author whiliang
 * @version V1.0
 * @Title: MybatisGenerator
 * @Package PACKAGE_NAME
 * @Description: Mybatis文件生成器
 */
public class MybatisGenerator {

    private static final Logger logger = LoggerFactory.getLogger(MybatisGenerator.class);

    public static void main(String[] args) {
        String configFilePath = "mybatisGenerator.xml";
        if (OsInfo.isLinux()) {
            configFilePath = "mybatis/mybatisGenerator-linux.xml";
        } else if (OsInfo.isWindows()) {
            configFilePath = "mybatisGenerator-windows.xml";
        }
        try {
            List<String> warnings = new ArrayList<String>();
            /**不覆盖文件**/
            boolean overwrite = false;
            String path = MybatisGenerator.class.getResource("/").getFile();
            path = path.replaceAll("test-classes", "classes") + configFilePath;
            File configFile = new File(path);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            logger.error("执行MybatisGenerator遇到问题:" + e.getMessage(), e);
        }
    }

}
